//
//  RecentRowView.swift
//  HeroAnimation
//
//  Created by qingzhou on 2021/5/13.
//

import SwiftUI

struct RecentRowView: View {
    var recent:Profile
    @EnvironmentObject var profileData:ProfileDetailModel
    var animation: Namespace.ID
    var body: some View {
        HStack(spacing: 15) {
            Button(action:{
                withAnimation (.spring()){
                    profileData.selectedProfile = recent
                    profileData.showProfile.toggle()
                }
            }) {
                if profileData.showProfile {
                    Image(recent.profile)
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 60, height: 60)
                        .clipShape(Circle())
                } else {
                    Image(recent.profile)
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .matchedGeometryEffect(id: recent.id, in: animation)
                        .frame(width: 60, height: 60)
                        .clipShape(Circle())
                }
                
            }
            .buttonStyle(PlainButtonStyle())
            VStack{
                HStack {
                    VStack(alignment: .leading, spacing: 0, content: {
                        Text(recent.userName)
                            .fontWeight(.bold)
                        Text(recent.lastMsg)
                            .font(.caption)
                            .foregroundColor(.gray)
                    })
                    Spacer()
                    
                    Text(recent.time)
                        .font(.caption2)
                        .foregroundColor(.gray)
                }
                Divider()
            }
        }
        .padding(.horizontal)
    }
}

struct RecentRowView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
